#!/usr/bin/env node

const fs  = require('fs'),
      src = process.argv[2],
      dst = process.argv[3];

//源文件不存在打印错误信息
if(!fs.existsSync(src)){
  console.error('%s not exist!',src);
  process.exit(1);
}

//命令行参数不是文件名称打印错误信息
if(!fs.statSync(src).isFile()){
  console.error('%s is not file!',src);
  process.exit(2);
}

//目标文件名undefined打印错误信息
if(typeof dst === 'undefined'){
  console.error('dst is undefined');
  process.exit(3);
}

//复制文件内容
//let stm = fs.createReadStream(src).pipe(fs.createWriteStream(dst));

let srcStream = fs.createReadStream(src),
    dstStream = fs.createWriteStream(dst);

srcStream.pipe(dstStream);

//复制文件权限
dstStream.on('close',()=>{
  fs.chmodSync(dst,fs.statSync(src).mode);
});



